﻿using System.Data;

namespace DAL.DataProcess;

public interface IDataAccess : IDisposable
{
    smErrorCode Initialize(string connectionStr);

    smErrorCode Start();

    smErrorCode Stop();

    #region 同步

    /// <summary>
    /// 通过主键获取实体对象
    /// </summary>
    /// <param name="id">主键ID</param>
    /// <returns></returns>
    T Get<T, TKey>(TKey id);

    /// <summary>
    /// 条件获取第一个数据
    /// </summary>
    /// <param name="conditions"></param>
    /// <param name="parameters"></param>
    /// <returns></returns>
    T Get<T>(string conditions, object parameters = null);

    /// <summary>
    /// 获取所有的数据
    /// </summary>
    /// <returns></returns>
    IEnumerable<T> GetList<T>();

    /// <summary>
    /// 执行具有条件的查询，并将结果映射到强类型列表
    /// </summary>
    /// <param name="whereConditions">条件</param>
    /// <returns></returns>
    IEnumerable<T> GetList<T>(object whereConditions) where T : class;

    /// <summary>
    /// 带参数的查询满足条件的数据
    /// </summary>
    /// <param name="conditions">条件</param>
    /// <param name="parameters">参数</param>
    /// <returns></returns>
    IEnumerable<T> GetList<T>(string conditions, object parameters = null);

    /// <summary>
    /// 使用where子句执行查询，并将结果映射到具有Paging的强类型List
    /// </summary>
    /// <param name="pageNumber">页码</param>
    /// <param name="rowsPerPage">每页显示数据</param>
    /// <param name="conditions">查询条件</param>
    /// <param name="orderBy">排序</param>
    /// <param name="parameters">参数</param>
    /// <returns></returns>
    IEnumerable<T> GetListPaged<T>(int pageNumber, int rowsPerPage, string conditions, string orderBy, object parameters = null);

    /// <summary>
    /// 插入一条记录并返回主键值(自增类型返回主键值，否则返回null)扩展方法DapperExtensions
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    int? Insert<T>(T entity) where T : class;

    /// <summary>
    /// 插入一条记录并返回主键值(自增类型返回主键值，否则返回null)原生的
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="entity"></param>
    /// <returns></returns>
    int? InsertOri<T>(T entity) where T : class;

    TKey Insert<TKey, T>(TKey id, T entity);

    /// <summary>
    /// 批量插入
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="entities"></param>
    /// <param name="transaction"></param>
    void BatchInsert<T>(IEnumerable<T> entities, IDbTransaction transaction = null) where T : class;

    /// <summary>
    /// 更新一条数据并返回影响的行数
    /// </summary>
    /// <param name="entity"></param>
    /// <returns>影响的行数</returns>
    bool Update<T>(T entity) where T : class;

    /// <summary>
    /// 删除一条数据
    /// </summary>
    /// <param name="entity">实体</param>
    /// <returns>返回影响的行数</returns>
    int Delete<T>(T entity);

    /// <summary>
    /// 条件删除多条记录
    /// </summary>
    /// <param name="whereConditions">条件</param>
    /// <param name="transaction">事务</param>
    /// <param name="commandTimeout">超时时间</param>
    /// <returns>影响的行数</returns>
    bool DeleteList<T>(object whereConditions, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;

    /// <summary>
    /// 使用where子句删除多个记录
    /// </summary>
    /// <param name="conditions">wher子句</param>
    /// <param name="parameters">参数</param>
    /// <param name="transaction">事务</param>
    /// <param name="commandTimeout">超时时间</param>
    /// <returns>影响的行数</returns>
    int DeleteList<T>(string conditions, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null);

    /// <summary>
    /// 满足条件的记录数量
    /// </summary>
    /// <param name="conditions"></param>
    /// <param name="parameters"></param>
    /// <returns></returns>
    int RecordCount<T>(string conditions = "", object parameters = null);

    #endregion 同步

    #region 异步

    /// <summary>
    /// 通过主键获取实体对象
    /// </summary>
    /// <param name="id">主键ID</param>
    /// <returns></returns>
    Task<T> GetAsync<T, TKey>(TKey id);

    /// <summary>
    /// 条件获取第一个
    /// </summary>
    /// <param name="conditions"></param>
    /// <param name="parameters"></param>
    /// <returns></returns>
    Task<T> GetAsync<T>(string conditions, object parameters = null);

    /// <summary>
    /// 获取所有的数据
    /// </summary>
    /// <returns></returns>
    Task<IEnumerable<T>> GetListAsync<T>();

    /// <summary>
    /// 执行具有条件的查询，并将结果映射到强类型列表
    /// </summary>
    /// <param name="whereConditions">条件</param>
    /// <returns></returns>
    Task<IEnumerable<T>> GetListAsync<T>(object whereConditions);

    /// <summary>
    /// 带参数的查询满足条件的数据
    /// </summary>
    /// <param name="conditions">条件</param>
    /// <param name="parameters">参数</param>
    /// <returns></returns>
    Task<IEnumerable<T>> GetListAsync<T>(string conditions, object parameters = null);

    /// <summary>
    /// 使用where子句执行查询，并将结果映射到具有Paging的强类型List
    /// </summary>
    /// <param name="pageNumber">页码</param>
    /// <param name="rowsPerPage">每页显示数据</param>
    /// <param name="conditions">查询条件</param>
    /// <param name="orderby">排序</param>
    /// <param name="parameters">参数</param>
    /// <returns></returns>
    Task<IEnumerable<T>> GetListPagedAsync<T>(int pageNumber, int rowsPerPage, string conditions, string orderby, object parameters = null);

    /// <summary>
    /// 插入一条记录并返回主键值
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    Task<int?> InsertAsync<T>(T entity);

    /// <summary>
    /// 更新一条数据并返回影响的行数
    /// </summary>
    /// <param name="entity"></param>
    /// <returns>影响的行数</returns>
    Task<int> UpdateAsync<T>(T entity);

    /// <summary>
    /// 根据实体删除一条数据
    /// </summary>
    /// <param name="entity">实体</param>
    /// <returns>返回影响的行数</returns>
    Task<int> DeleteAsync<T>(T entity);

    /// <summary>
    /// 条件删除多条记录
    /// </summary>
    /// <param name="whereConditions">条件</param>
    /// <param name="transaction">事务</param>
    /// <param name="commandTimeout">超时时间</param>
    /// <returns>影响的行数</returns>
    Task<int> DeleteListAsync<T>(object whereConditions, IDbTransaction transaction = null, int? commandTimeout = null);

    /// <summary>
    /// 使用where子句删除多个记录
    /// </summary>
    /// <param name="conditions">wher子句</param>
    /// <param name="parameters">参数</param>
    /// <param name="transaction">事务</param>
    /// <param name="commandTimeout">超时时间</param>
    /// <returns>影响的行数</returns>
    Task<int> DeleteListAsync<T>(string conditions, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null);

    /// <summary>
    /// 满足条件的记录数量
    /// </summary>
    /// <param name="conditions"></param>
    /// <param name="parameters"></param>
    /// <returns></returns>
    Task<int> RecordCountAsync<T>(string conditions = "", object parameters = null);

    #endregion 异步

    #region 同步--原始SQL

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    IEnumerable<T> Query<T>(string sql, object param = null, IDbTransaction transaction = null,
        bool buffered = true, int? commandTimeout = null, CommandType? commandType = null);

    /// <summary>
    /// 联合查询--2张表
    /// </summary>
    /// <typeparam name="TFirst"></typeparam>
    /// <typeparam name="TSecond"></typeparam>
    /// <typeparam name="TReturn"></typeparam>
    /// <param name="sql"></param>
    /// <param name="map"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="splitOn"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    IEnumerable<TReturn> Query<TFirst, TSecond, TReturn>(string sql,
        Func<TFirst, TSecond, TReturn> map,
        object param = null,
        IDbTransaction transaction = null,
        bool buffered = true,
        string splitOn = "Id",
        int? commandTimeout = null,
        CommandType? commandType = null);

    /// <summary>
    /// 联合查询--3张表
    /// </summary>
    /// <typeparam name="TFirst"></typeparam>
    /// <typeparam name="TSecond"></typeparam>
    /// <typeparam name="TThird"></typeparam>
    /// <typeparam name="TReturn"></typeparam>
    /// <param name="sql"></param>
    /// <param name="map"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="splitOn"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TReturn>(string sql,
        Func<TFirst, TSecond, TThird, TReturn> map, object param = null,
        IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null,
        CommandType? commandType = null);

    /// <summary>
    /// 联合查询--4张表
    /// </summary>
    /// <typeparam name="TFirst"></typeparam>
    /// <typeparam name="TSecond"></typeparam>
    /// <typeparam name="TThird"></typeparam>
    /// <typeparam name="TFourth"></typeparam>
    /// <typeparam name="TReturn"></typeparam>
    /// <param name="sql"></param>
    /// <param name="map"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="splitOn"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TReturn>(string sql,
        Func<TFirst, TSecond, TThird, TFourth, TReturn> map, object param = null,
        IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null,
        CommandType? commandType = null);

    /// <summary>
    /// 联合查询--5张表
    /// </summary>
    /// <typeparam name="TFirst"></typeparam>
    /// <typeparam name="TSecond"></typeparam>
    /// <typeparam name="TThird"></typeparam>
    /// <typeparam name="TFourth"></typeparam>
    /// <typeparam name="TFifth"></typeparam>
    /// <typeparam name="TReturn"></typeparam>
    /// <param name="sql"></param>
    /// <param name="map"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="splitOn"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(string sql,
        Func<TFirst, TSecond, TThird, TFourth, TFifth, TReturn> map, object param = null,
        IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null,
        CommandType? commandType = null);

    /// <summary>
    /// 联合查询--6张表
    /// </summary>
    /// <typeparam name="TFirst"></typeparam>
    /// <typeparam name="TSecond"></typeparam>
    /// <typeparam name="TThird"></typeparam>
    /// <typeparam name="TFourth"></typeparam>
    /// <typeparam name="TFifth"></typeparam>
    /// <typeparam name="TSixth"></typeparam>
    /// <typeparam name="TReturn"></typeparam>
    /// <param name="sql"></param>
    /// <param name="map"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="splitOn"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TReturn>(string sql,
        Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TReturn> map, object param = null,
        IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null,
        CommandType? commandType = null);

    /// <summary>
    /// 联合查询--7张表
    /// </summary>
    /// <typeparam name="TFirst"></typeparam>
    /// <typeparam name="TSecond"></typeparam>
    /// <typeparam name="TThird"></typeparam>
    /// <typeparam name="TFourth"></typeparam>
    /// <typeparam name="TFifth"></typeparam>
    /// <typeparam name="TSixth"></typeparam>
    /// <typeparam name="TSeventh"></typeparam>
    /// <typeparam name="TReturn"></typeparam>
    /// <param name="sql"></param>
    /// <param name="map"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="splitOn"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn>(string sql,
        Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn> map, object param = null,
        IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null,
        CommandType? commandType = null);

    /// <summary>
    /// 联合查询--使用任意数量的输入类型执行多映射查询
    /// </summary>
    /// <typeparam name="TReturn"></typeparam>
    /// <param name="sql"></param>
    /// <param name="types"></param>
    /// <param name="map"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="splitOn"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    IEnumerable<TReturn> Query<TReturn>(string sql, Type[] types, Func<object[], TReturn> map, object param = null,
        IDbTransaction transaction = null,
        bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null);

    /// <summary>
    /// 操作
    /// </summary>
    /// <param name="cnn"></param>
    /// <param name="sql"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    int Execute(
        string sql,
        object param = null,
        IDbTransaction transaction = null,
        int? commandTimeout = null,
        CommandType? commandType = null);

    #endregion 同步--原始SQL

    #region 异步--原始SQL

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>
    Task<IEnumerable<T>> QueryAsync<T>(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null);

    /// <summary>
    /// 联合查询--2张表
    /// </summary>
    /// <typeparam name="TFirst"></typeparam>
    /// <typeparam name="TSecond"></typeparam>
    /// <typeparam name="TReturn"></typeparam>
    /// <param name="sql"></param>
    /// <param name="map"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="splitOn"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TReturn>(string sql,
        Func<TFirst, TSecond, TReturn> map,
        object param = null,
        IDbTransaction transaction = null,
        bool buffered = true,
        string splitOn = "Id",
        int? commandTimeout = null,
        CommandType? commandType = null);

    /// <summary>
    /// 联合查询--3张表
    /// </summary>
    /// <typeparam name="TFirst"></typeparam>
    /// <typeparam name="TSecond"></typeparam>
    /// <typeparam name="TThird"></typeparam>
    /// <typeparam name="TReturn"></typeparam>
    /// <param name="sql"></param>
    /// <param name="map"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="splitOn"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TThird, TReturn>(string sql,
        Func<TFirst, TSecond, TThird, TReturn> map, object param = null,
        IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null,
        CommandType? commandType = null);

    /// <summary>
    /// 联合查询--4张表
    /// </summary>
    /// <typeparam name="TFirst"></typeparam>
    /// <typeparam name="TSecond"></typeparam>
    /// <typeparam name="TThird"></typeparam>
    /// <typeparam name="TFourth"></typeparam>
    /// <typeparam name="TReturn"></typeparam>
    /// <param name="sql"></param>
    /// <param name="map"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="splitOn"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TThird, TFourth, TReturn>(string sql,
        Func<TFirst, TSecond, TThird, TFourth, TReturn> map, object param = null,
        IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null,
        CommandType? commandType = null);

    /// <summary>
    /// 联合查询--5张表
    /// </summary>
    /// <typeparam name="TFirst"></typeparam>
    /// <typeparam name="TSecond"></typeparam>
    /// <typeparam name="TThird"></typeparam>
    /// <typeparam name="TFourth"></typeparam>
    /// <typeparam name="TFifth"></typeparam>
    /// <typeparam name="TReturn"></typeparam>
    /// <param name="sql"></param>
    /// <param name="map"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="splitOn"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(string sql,
        Func<TFirst, TSecond, TThird, TFourth, TFifth, TReturn> map, object param = null,
        IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null,
        CommandType? commandType = null);

    /// <summary>
    /// 联合查询--6张表
    /// </summary>
    /// <typeparam name="TFirst"></typeparam>
    /// <typeparam name="TSecond"></typeparam>
    /// <typeparam name="TThird"></typeparam>
    /// <typeparam name="TFourth"></typeparam>
    /// <typeparam name="TFifth"></typeparam>
    /// <typeparam name="TSixth"></typeparam>
    /// <typeparam name="TReturn"></typeparam>
    /// <param name="sql"></param>
    /// <param name="map"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="splitOn"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TReturn>(string sql,
        Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TReturn> map, object param = null,
        IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null,
        CommandType? commandType = null);

    /// <summary>
    /// 联合查询--7张表
    /// </summary>
    /// <typeparam name="TFirst"></typeparam>
    /// <typeparam name="TSecond"></typeparam>
    /// <typeparam name="TThird"></typeparam>
    /// <typeparam name="TFourth"></typeparam>
    /// <typeparam name="TFifth"></typeparam>
    /// <typeparam name="TSixth"></typeparam>
    /// <typeparam name="TSeventh"></typeparam>
    /// <typeparam name="TReturn"></typeparam>
    /// <param name="sql"></param>
    /// <param name="map"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="splitOn"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn>(string sql,
        Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn> map, object param = null,
        IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null,
        CommandType? commandType = null);

    /// <summary>
    /// 联合查询--使用任意数量的输入类型执行多映射查询
    /// </summary>
    /// <typeparam name="TReturn"></typeparam>
    /// <param name="sql"></param>
    /// <param name="types"></param>
    /// <param name="map"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="splitOn"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    Task<IEnumerable<TReturn>> QueryAsync<TReturn>(string sql, Type[] types, Func<object[], TReturn> map, object param = null,
        IDbTransaction transaction = null,
        bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null);

    /// <summary>
    /// 操作
    /// </summary>
    /// <param name="cnn"></param>
    /// <param name="sql"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    Task<int> ExecuteAsync(
        string sql,
        object param = null,
        IDbTransaction transaction = null,
        int? commandTimeout = null,
        CommandType? commandType = null);

    #endregion 异步--原始SQL

    #region 事务

    IDbTransaction GetTransaction();

    #endregion 事务
}